package parkshop.pmcourse.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yaml.snakeyaml.scanner.Constant;

import parkshop.pmcourse.domain.Advertisement;
import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.service.AdvertisementService;
import parkshop.pmcourse.service.ProductService;
import parkshop.pmcourse.service.SearchService;
import parkshop.pmcourse.service.ShopService;
import parkshop.pmcourse.utils.ConstantUtil;

@Controller
@RequestMapping("/product")
public class ProductContrller {

	@Autowired
	private ProductService productService;

	@Autowired
	private ShopService shopService;

	@Autowired
	private SearchService searchService;
	@Autowired
	private AdvertisementService advertisementService;
	
	
	@GetMapping("/toProductDetail/{productId}/{commentNumber}")
	public String toProductDetail(@PathVariable Long productId,@PathVariable int commentNumber, Model model) {
		Product product = productService.getProductById(productId);
		Long shopId = product.getShopId();
		Shop shop = shopService.getShopById(shopId);
		PageBean commentList=productService.getProductNewComment(productId,commentNumber);
		model.addAttribute("product", product);
		model.addAttribute("shop", shop);
		model.addAttribute("commentPageBean", commentList);
		return "productDetail";
	}


	@PostMapping("/search/name")
	public String searchProduct(Long shopId,String name,Model model){	
		List<Advertisement> ADList=advertisementService.getAdvertisementsByShopId((long) shopId);
		model.addAttribute("ADList", ADList);
		
		PageBean productList=searchService.searchPageProductByName(name, 1);
		model.addAttribute("productPage", productList);
		return "index";
	}

	
}
